<?php

namespace App\Services\Bank\BankOfHangzhou;

class BankOfHangzhouConfig
{
    /**
     * API基础配置
     */
    // public const API_URL = 'http://test.hccb.cc:9981/BisOutWeb9981/payCenter/dealReq.srv';

    /**
     * 默认配置
     */
    public const DEFAULT_CONFIG = [
        'timeout' => 30,
        'charset' => 'UTF-8',
        'message_type' => '01',
        'with_header' => false, // 测试环境无需报文头
    ];

    /**
     * 交易码定义
     */
    public const TRADE_CODES = [
        'BANK_SYNC' => 'BY0012',           // 联行号增量同步
        'BANK_QUERY' => 'BY0004',          // 联行号查询
        'BALANCE_QUERY' => 'BY0007',       // 余额查询
        'BATCH_PAY' => 'BY0008',           // 批量代发
        'PAY_RESULT_QUERY' => 'BY0009',    // 代发结果查询
        'RECEIPT_BATCH_APPLY' => 'BY0010', // 回单批量申请
        'RECEIPT_DOWNLOAD' => 'BY0066',    // 单笔回单下载
        'DAILY_DETAIL_QUERY' => 'BY0005',  // 当日明细查询
        'HISTORY_DETAIL_QUERY' => 'BY0006', // 历史明细查询
    ];

    /**
     * 禁用字符集
     */
    public const FORBIDDEN_CHARS = ['\\', '!', '@', '#', '$', '%', '^', '&', '*', '+', '=', '/', '?', '<', '>', '`', '~', ';', ','];

    /**
     * 错误码定义
     */
    public const ERROR_CODES = [
        '0' => '交易成功',
        '2' => '交易失败',
    ];

    /**
     * 业务类型定义
     */
    public const BUSINESS_TYPES = [
        '1' => '工资/代发工资',
        '2' => '股权基金分红/代发差旅费',
        '3' => '其他/报销',
    ];

    /**
     * 更新类型定义
     */
    public const UPDATE_TYPES = [
        '1' => '新增',
        '2' => '修改',
        '3' => '删除',
    ];

    /**
     * 付款流程定义
     */
    public const PAY_FLOWS = [
        '1' => '待审批',
        '2' => '指令发送',
    ];

    private string $erpSysCode;
    private string $custNo;
    private array $config;

    public function __construct(string $erpSysCode, string $custNo, array $config = [])
    {
        $this->erpSysCode = $erpSysCode;
        $this->custNo = $custNo;
        $this->config = array_merge(self::DEFAULT_CONFIG, $config);
    }

    public function getErpSysCode(): string
    {
        return $this->erpSysCode;
    }

    public function getCustNo(): string
    {
        return $this->custNo;
    }

    public function getConfig(string $key = null)
    {
        if ($key === null) {
            return $this->config;
        }

        return $this->config[$key] ?? null;
    }

    public function getApiUrl(): string
    {
        return config('bank.banks.BankOfHangzhou.api_url');
    }

    public function getTradeCode(string $operation): string
    {
        return self::TRADE_CODES[$operation] ?? '';
    }

    /**
     * 验证字符串中是否包含禁用字符
     */
    public function validateString(string $input): bool
    {
        foreach (self::FORBIDDEN_CHARS as $char) {
            if (str_contains($input, $char)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 清理字符串中的禁用字符
     */
    public function cleanString(string $input): string
    {
        return str_replace(self::FORBIDDEN_CHARS, '', $input);
    }
}
